/*
 * @Author       : NaN 353362604@qq.com
 * @Date         : 2023-01-12 09:35:49
 * @LastEditTime : 2023-02-28 09:49:17
 * @LastEditors  : NaN 353362604@qq.com
 * @FilePath     : \AdminStration\src\Pages\Front\Common\LargeScreen\ButtonList.jsx
 * @Description  :
 */
import { Button, message } from 'antd'
import { useRequest, useBoolean } from 'ahooks'
import { Http } from '../../../../Common/Api'
import { useState } from 'react'
import { AttachmentList, AttachmentDetail, InspectionDetailsBox } from './AttachmentWindow'

// 附件详情
export const TableViewAttachmentsBtn = ({ data, title, className, api, params }) => {
    const [ListState, { setTrue: ListStateTrue, setFalse: ListStateFalse }] = useBoolean(false)
    const [ViewState, { setTrue: ViewStateTrue, setFalse: ViewStateFalse }] = useBoolean(false)
    const [AttList, setAttList] = useState()
    const { runAsync: getInfo } = useRequest(Http, {
        manual: true,
        onSuccess: ({ list }) => {
            if (list.length === 0) {
                message.info('没有附件信息!')
            } else if (list.length === 1) {
                const [data] = list
                setAttList(data)
                ViewStateTrue()
            } else {
                setAttList(list)
                ListStateTrue()
            }
        },
    })

    return (
        <>
            {!params ? (
                <Button
                    type="link"
                    onClick={() => {
                        setAttList(data)
                        ViewStateTrue()
                    }}
                >
                    {title}
                </Button>
            ) : (
                <Button type="link" onClick={() => getInfo({ url: api, data: params })}>
                    {title}
                </Button>
            )}
            {ListState && AttList && <AttachmentList data={AttList} className={className} Off={ListStateFalse} />}
            {ViewState && AttList && <AttachmentDetail data={AttList} className={className} Off={ViewStateFalse} />}
        </>
    )
}

// 安全巡检
export const InspectionDetailsBtn = ({ title, data, className, type }) => {
    const [DetailsState, { setTrue, setFalse }] = useBoolean(false)
    return (
        <>
            <Button type="link" onClick={setTrue}>
                {title}
            </Button>
            {DetailsState && <InspectionDetailsBox data={data} className={className} Off={setFalse} type={type} />}
        </>
    )
}
